package com.truongnha.clienttn.data.dao;

import java.util.ArrayList;
import java.util.List;

import com.truongnha.clienttn.data.entity.StudentMarkEntity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class StudentMarkDAO {
	private final SQLiteDatabase db;

	public StudentMarkDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}

	public long insert(int mark_id, int number, String score, String time) {
		ContentValues inititalValue = new ContentValues();
		inititalValue.put(StudentMarkScheme.MARK_ID, mark_id);
		inititalValue.put(StudentMarkScheme.NUMBER, number);
		if (score.equals(""))
			return 0;
		inititalValue.put(StudentMarkScheme.SCORE, score);
		inititalValue.put(StudentMarkScheme.TIME, time);
		return db.insert(StudentMarkScheme.TABLE_NAME, null, inititalValue);
	}

	public void delete() {
		db.delete(StudentMarkScheme.TABLE_NAME, null, null);
	}

	public List<StudentMarkEntity> getAll() {
		String sql = "SELECT * from " + StudentMarkScheme.TABLE_NAME;
		Cursor mcursor = db.rawQuery(sql, null);
		List<StudentMarkEntity> list = new ArrayList<StudentMarkEntity>();

		if (mcursor.getCount() != 0) {
			mcursor.moveToFirst();
			while (!mcursor.isAfterLast()) {
				StudentMarkEntity studentMarkEntity = new StudentMarkEntity(
						mcursor.getInt(0), mcursor.getInt(1),
						mcursor.getInt(2), mcursor.getString(3),
						mcursor.getString(4));
				list.add(studentMarkEntity);
				mcursor.moveToNext();
			}
		}
		mcursor.close();
		return list;
	}

	public List<StudentMarkEntity> getByTime() {
		String sql = "SELECT * from " + StudentMarkScheme.TABLE_NAME
				+ " order by time DESC";
		Cursor mcursor = db.rawQuery(sql, null);
		List<StudentMarkEntity> list = new ArrayList<StudentMarkEntity>();
		if (mcursor.getCount() != 0) {
			mcursor.moveToFirst();
			while (!mcursor.isAfterLast()) {
				StudentMarkEntity studentMarkEntity = new StudentMarkEntity(
						mcursor.getInt(0), mcursor.getInt(1),
						mcursor.getInt(2), mcursor.getString(3),
						mcursor.getString(4));
				list.add(studentMarkEntity);
				mcursor.moveToNext();
			}
		}
		mcursor.close();
		return list;
	}
}
